<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/14/13
 * Time: 8:10 PM
 * To change this template use File | Settings | File Templates.
 */

define('APP_ROOT', realpath(__DIR__ . '/../..'));

require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/persistance/DBConnector.php');
require_once(APP_ROOT . '/modules/persistance/InventoryObjectPersistance.php');

use persistance\DBConnector;
use persistance\InventoryObjectPersistance;
use common\Errors;

function testFindInventoryObjectByExistingId() {
    $dbconn = new DBConnector();
    $dbconn->connect();

    $ip     = new InventoryObjectPersistance();
    if(!$ip->init($dbconn)) {
        print('testFindInventoryObjectByExistingId(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $ip->findById(1);

    print('testFindInventoryObjectByExistingId(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_NONE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testFindInventoryObjectByNotExistingId() {
    $dbconn = new DBConnector();
    $dbconn->connect();

    $ip     = new InventoryObjectPersistance();
    if(!$ip->init($dbconn)) {
        print('testFindInventoryObjectByExistingId(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $ip->findById(9999999999);

    print('testFindInventoryObjectByNotExistingId(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_PERSISTANCE_NOT_FOUND) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testFindInventoryObjectByNameLike() {
    $dbconn = new DBConnector();
    $dbconn->connect();

    $ip = new InventoryObjectPersistance();
    if(!$ip->init($dbconn)) {
        print('testFindInventoryObjectByExistingId(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $ip->findByName('cla', true);

    print('testFindInventoryObjectByNameLike(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_NONE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

testFindInventoryObjectByExistingId();
testFindInventoryObjectByNotExistingId();
testFindInventoryObjectByNameLike();
